<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\finance\cost;

use app\enum\CommonEnum;
use app\model\AccountDailyStatModel;
use app\service\BaseService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

abstract class CostAccountService extends BaseService implements CostInterface
{
    /**
     * 获取消耗.
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getCost(string $advertiserId = '', int $startDate = 0, int $endDate = 0): array
    {
        $where = [
            ['advertiser_id', '=', $advertiserId],
            ['stat_date', '>=', $startDate],
            ['stat_date', '<=', $endDate],
        ];
        //        $data = static::getModel()->where($where)->select();
        $data = (new AccountDailyStatModel())->where($where)->select();
        $cost = 0;  // 总消耗
        $cashCost = 0;  // 现金
        $rewardCost = 0;  // 赠款
        foreach ($data as $costModel) {
            $cost += $costModel->getAttr('cost');
            $cashCost += $costModel->getAttr('cash_cost');
            if ($costModel->getAttr('media_id') == CommonEnum::QQ_MEDIA) {
                $rewardCost += ($costModel->getAttr('cost') - $costModel->getAttr('cash_cost'));
            } else {
                $rewardCost += $costModel->getAttr('reward_cost');
            }

        }
        return [
            'cost' => $cost,
            'cash_cost' => $cashCost,
            'reward_cost' => $rewardCost,
        ];
    }
}
